package heven.holt.wan.android.extensions

import heven.holt.library.toast.toast
import heven.holt.wan.android.model.BaseModel
import kotlinx.coroutines.coroutineScope

suspend inline fun <V> requestCoroutine(crossinline api: suspend () -> BaseModel<V>): V? =
    coroutineScope {
        try {
            val response: BaseModel<V> = api.invoke()
            if (response.isSuccess) {
                response.data
            } else {
                toast(response.errorMsg)
                null
            }
        } catch (exception: Exception) {
            null
        }
    }